﻿using System;
using System.IO;
using System.Web;
using System.Linq;
using System.Data;
using System.Reflection;
using System.Collections.Specialized;
using System.Collections.Generic;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

using server.Logic;
using WAF.EntityFramework;

namespace server.Remote
{
    public class GeneralGrid
    {
        /// <summary>
        /// 获取要显示的ExtGrid的列信息
        /// </summary>
        /// <param name="HttpContext">请求内容上下文</param>
        /// <returns>ExtGrid显示数据</returns>
        public string GetGeneralGridSchema(HttpContext context)
        {
            string entityName = AppEntry.GetPayloadParameter(context)[0];
            Type entityType   = Common.GetEntityType(entityName);

            GeneralGridSchema gridSchema = new GeneralGridSchema();


            foreach (PropertyInfo pInfo in EntitySession.GetPropertiesOrder(entityType))
            {
                EntityFieldAttribute fAttribute = EntitySession.GetFieldCustomAttributes(pInfo);

                if (fAttribute.IsDisplay)
                {
                    gridSchema.ModelFields.Add(new GeneralModelField() { name = pInfo.Name });
                    gridSchema.GridColumns.Add(new GeneralGridColumn() { header = "<div style='text-align:center;font-weight:bold'>" + fAttribute.Label + "</div>", dataIndex = pInfo.Name });
                }
            }

            return AppEntry.WriteSuccessResult(true, JsonConvert.SerializeObject(gridSchema));
        }

        /// <summary>
        /// 获取要显示的Grid的数据
        /// </summary>
        /// <param name="HttpContext">请求内容上下文</param>
        /// <returns>Grid显示数据</returns>
        public string GetGeneralGridData(HttpContext context)
        {
            NameValueCollection parms = context.Request.QueryString;
            Type entityType = Common.GetEntityType(parms["entityName"]);

            int total = 0;
            GeneralGridData gridData = new GeneralGridData();

            gridData.Data = EntityAction.GetGridData(entityType, parms["searchText"], out total, false, int.Parse(parms["page"]), int.Parse(parms["limit"]));
            gridData.Total = total;

            return JsonConvert.SerializeObject(gridData);
        }

        /**************** 输出json格式类 ****************/

        /// <summary>
        /// GeneralGrid结构信息
        /// </summary>
        private class GeneralGridSchema
        {
            public GeneralGridSchema()
            {
                ModelFields = new List<GeneralModelField>();
                GridColumns = new List<GeneralGridColumn>();
            }

            /// <summary>
            /// 对应实体的Model字段列表
            /// </summary>
            public List<GeneralModelField> ModelFields { get; set; }

            /// <summary>
            /// GeneralGrid的列信息列表
            /// </summary>
            public List<GeneralGridColumn> GridColumns { get; set; }
        }

        /// <summary>
        /// 对应实体的Model字段
        /// </summary>
        private class GeneralModelField
        {
            /// <summary>
            /// Model字段名称
            /// </summary>
            public string name { get; set; }
        }

        /// <summary>
        /// GeneralGrid的列信息
        /// </summary>
        private class GeneralGridColumn
        {
            /// <summary>
            /// 表头标题
            /// </summary>
            public string header { get; set; }

            /// <summary>
            /// 数据索引
            /// </summary>
            public string dataIndex { get; set; }
        }

        /// <summary>
        /// GeneralGrid的数据
        /// </summary>
        private class GeneralGridData
        {
            /// <summary>
            /// 记录总条数
            /// </summary>
            public int Total;

            /// <summary>
            /// 数据
            /// </summary>
            [JsonConverter(typeof(DataTableConverter))]
            public DataTable Data;
        }
    }
}